package edu.iss.javaproject.controller;

import java.util.Map;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import edu.iss.javaproject.exception.LeaveNotFoundException;
import edu.iss.javaproject.exception.UserNotFoundException;
import edu.iss.javaproject.model.LeaveRecord;
import edu.iss.javaproject.model.Users;
import edu.iss.javaproject.service.LAPService;
import edu.iss.javaproject.service.LeaveService;
import edu.iss.javaproject.util.SendMailTLS;
import edu.iss.javaproject.validator.LeaveValidator;


@Controller
@SessionAttributes("sessionuser")
@RequestMapping(value = "staffHome/leave")

public class LeaveController {

private static final Logger logger = LoggerFactory.getLogger(LeaveController.class);
@Autowired
private LAPService lapservice;
		
@Autowired
private LeaveService leaveService;

@Autowired
private LeaveValidator leaveValidator;

// Apply for leave
@RequestMapping(value = "/apply", method = RequestMethod.GET)
public ModelAndView applyLeavePage(){
	ModelAndView mav = new ModelAndView("leaveApply", "leaveRecord", new LeaveRecord());
	return mav;
}
				
@RequestMapping(value = "/apply", method = RequestMethod.POST, params = "apply")
public ModelAndView applyLeave(@ModelAttribute @Valid LeaveRecord leaveRecord, BindingResult result,
		@ModelAttribute("sessionuser") Users u, final RedirectAttributes redirectAttributes, Model model) throws UserNotFoundException{
	Users user=u;
	
	String message = "Error in input. Please try again";
	
	ModelAndView mav = new ModelAndView("leaveApply");
	leaveRecord.setUsers(user);
	if(result.hasErrors()){
		model.addAttribute("message", message);
		return mav;
	} else {
		if(leaveValidator.checkDates(leaveRecord) == false){
			model.addAttribute("message", message);
			return mav;
		} else {
			System.out.println(leaveRecord.getLeaveType());
			if(leaveRecord.getLeaveType().equals("Medical")){
				
				if(leaveService.validateMedical(leaveRecord) == -1){
					model.addAttribute("message", message);
					return mav;
				} else {
					user.setMedical(leaveService.validateMedical(leaveRecord));
					lapservice.save(user);
					leaveRecord.setDuration(leaveService.durationMedical(leaveRecord));
					leaveRecord.setLeaveStatus("Applied");
					lapservice.save(leaveRecord);
				}
				
			} else if (leaveRecord.getLeaveType().equals("Annual")){
				
				if(leaveService.validateAnnual(leaveRecord) == -1){
					model.addAttribute("message", message);
					return mav;
				} else {
					user.setAnnual(leaveService.validateAnnual(leaveRecord));
					lapservice.save(user);
					leaveRecord.setDuration(leaveService.durationAnnual(leaveRecord));
					leaveRecord.setLeaveStatus("Applied");
					lapservice.save(leaveRecord);
				}
				
			} else {
				
				if(leaveService.validateCompensation(leaveRecord) == false){
					model.addAttribute("message", message);
					return mav;
				} else {
					user.setCompensation(leaveService.compensationDays(leaveRecord));
					lapservice.save(user);
					leaveRecord.setLeaveStatus("Applied");
					lapservice.save(leaveRecord);

				}
			}
		}
	}
	mav.setViewName("leaveSuccess");

	return mav;
}
				
				// Cancel leave for apply/update
				@RequestMapping(value = {"/apply", "/update"}, method = RequestMethod.POST, params = "cancel")
				public String cancelChange(){
					return "leaveUndo";
				}	
				
				// Update leave
				@RequestMapping(value = "/update", method = RequestMethod.GET)
				public ModelAndView updateLeavePage(@ModelAttribute @Valid LeaveRecord leaveRecord, BindingResult result) {
					ModelAndView mav;
					mav = new ModelAndView("leavelist");
					mav.addObject("leaveRecord", leaveRecord);
					return mav;
				}
				
				
				@RequestMapping(value = "/update", method = RequestMethod.POST)
				public ModelAndView updateLeave(@ModelAttribute @Valid LeaveRecord leaveRecord, BindingResult result, Model model) {
					
					ModelAndView mav;
					
					String message = "Error in input. Please try again";

					if(result.hasErrors()){
						model.addAttribute("message", message);
						return new ModelAndView("leaveUpdate");
					}					
					try {
						leaveRecord = lapservice.findByrefNO(leaveRecord.getRefNo());
						leaveRecord.setLeaveStatus("Updated");			
						lapservice.update(leaveRecord);
					} catch (LeaveNotFoundException e) {
						mav = new ModelAndView("ERROR");
					}
					mav = new ModelAndView("leaveSuccess");
					return mav;
				}
		}